package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import photoshop.Photoshop;

import localhost._8080.metaimage.Client;

import com.jidesoft.swing.CheckBoxList;

public class DeletionTab extends JPanel implements ListSelectionListener,KeyListener,MouseListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7825996244219155458L;

	
		private JList j;
		
		
		private ArrayList<String> listShapes;
		private CheckBoxList listCheckBox;
		private Vector<String> vectShapes;
		
		private JButton deleteImage;
		
		private JLabel hist;
		
		public DeletionTab(JList l){
			this.j=l;
			this.j.addListSelectionListener(this);
			this.setLayout(new BorderLayout());
			
			vectShapes = new Vector<String>();
			try{
				listShapes =  (ArrayList<String>) Client.p.getImageShapes(Client.id,((String) j.getSelectedValue()).split(".xml")[0]);			
				for (String i : listShapes){
					vectShapes.add(i.split(";")[0]);
					System.out.println();
				}		
				this.listCheckBox =  new CheckBoxList(vectShapes);
			}catch(Exception e){
				listShapes = new ArrayList<String>();
				this.listCheckBox =  new CheckBoxList();
			}
			
			
			//Ajout a un scrollpane
			JScrollPane scroll = new JScrollPane(listCheckBox);
			scroll.setBorder(BorderFactory.createTitledBorder("Shapes list" ));
			scroll.setPreferredSize(new Dimension(500,300));
			
			
			
			//Ajout d'un historique			;
			hist = new JLabel("<html>");			
			JScrollPane scrollHist = new JScrollPane(hist);
			scrollHist.setBorder(BorderFactory.createTitledBorder("History of removal" ));
			
			//Button
			deleteImage = new JButton("Delete image from the server");
			deleteImage.addMouseListener(this);
					
			JPanel panGauche = new JPanel();
			panGauche.setLayout(new BorderLayout());
			
			panGauche.add(deleteImage,BorderLayout.PAGE_START);
			panGauche.add(scroll,BorderLayout.CENTER);
			
			
			JPanel pan = new JPanel();
			pan.setLayout(new BorderLayout());
			pan.add(scrollHist,BorderLayout.CENTER);
		
			this.add(pan,BorderLayout.CENTER);
			this.add(panGauche,BorderLayout.LINE_START);
			
			this.listCheckBox.addKeyListener(this);
			//this.listCheckBox.addListSelectionListener(this);
			
			
		}
		
		
		public void valueChanged(ListSelectionEvent e) {	
			if (e.getSource() ==  this.j) updateList();
		}
		
		
		public void updateList(){
			
			//On vide les listes
			
				this.listShapes.clear();
				this.vectShapes.clear();
				this.listCheckBox.clearSelection();
				try {
					listShapes =  (ArrayList<String>) Client.p.getImageShapes(Client.id,((String) j.getSelectedValue()).split(".xml")[0]);
				}
				catch (Exception e){
					
				}
				for (String i : listShapes){
					vectShapes.add(i.split(";")[0]);
				}				
				this.listCheckBox.setListData(this.vectShapes);	
			
			
		}
		
		public void keyPressed(KeyEvent arg0) {
		
		}
		@Override
		public void keyReleased(KeyEvent arg0) {
		
		}
		@Override
		public void keyTyped(KeyEvent arg0) {
			
			if(arg0.getKeyChar() == '\n'){
				int toRemove;
				for (int i=0;i<this.listCheckBox.getCheckBoxListSelectedIndices().length;i++){					
					
					toRemove = this.listCheckBox.getCheckBoxListSelectedIndices()[i];
					this.hist.setText( this.hist.getText() + this.listShapes.get(toRemove).split(";")[0] + "<br>");
					
					System.out.println(this.listCheckBox.getCheckBoxListSelectedValues()[i]);
					Client.p.removeItem(Client.id,((String) j.getSelectedValue()).split(".xml")[0],
							this.listShapes.get(toRemove).split(";")[1]);
					
				}
				this.updateList();
				SplitPaneDemo.updateLabel(((String)j.getSelectedValue()));
			}
			
		}


	

		@Override
		public void mouseClicked(MouseEvent arg0) {
			if(arg0.getSource() == this.deleteImage){
				Client.execute("deleteItem "+((String)j.getSelectedValue()).split(".xml")[0]);
				SplitPaneDemo.updateList();
			}
			
		}


		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}


		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}


		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}


		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
		
		

	
		


		
		
}
